/**
 * ItemDAO.java
 * @author GorillaFunch
 * @created 2012.10.12
 */
package kr.ac.kongju.gedl.attach.dao;

import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import kr.ac.kongju.gedl.attach.vo.AttachFileVO;


import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import egovframework.rte.psl.dataaccess.EgovAbstractDAO;

@Repository("attachFileDAO")
public class AttachFileDAO extends EgovAbstractDAO{
	private final Logger logger = LoggerFactory.getLogger(getClass());
	
	
	/*************************************************************************
	 * AttachFileVO - 공지사항
	 */
	/**
	 * registerAttachFile
	 * AttachFile 등록
	 * @param attachFile
	 * @return
	 * @throws SQLException
	 */
	public AttachFileVO registerAttachFile(AttachFileVO attachFile){
		try{
			int result = (Integer)update("AttachFile.registerAttachFile", attachFile);
			
		}catch(Exception e){
			attachFile = null;
			logger.error("cause:{}", e.toString());
		}
		return attachFile;
	}
	
	/**
	 * retrieveAttachFile
	 * AttachFile 조회
	 * @param attachFile
	 * @return
	 * @throws SQLException
	 */
	public AttachFileVO retrieveAttachFile(AttachFileVO attachFile){
		try{
			attachFile = ((AttachFileVO)selectByPk("AttachFile.retrieveAttachFile", attachFile));
			
		}catch(Exception e){
			logger.error("cause:{}", e.toString()); 
		}
		return attachFile;
	}
	/**
	 * updateAttachFile
	 * AttachFile 수정
	 * @param attachFile
	 * @return
	 * @throws SQLException
	 */
	public int updateAttachFile(AttachFileVO attachFile){
		int result = 0;
		try{
			result = update("AttachFile.updateAttachFile", attachFile);
			logger.debug("AttachFile.updateAttachFile fileName:{} result:{}", attachFile.getAttachFileSeq(), result);
		}catch(Exception e){
			logger.error("cause:{}", e.toString());
		}
		return result;
	}
	
	/**
	 * updateListAttachFile
	 * AttachFile 수정
	 * @param attachFile(targetTable, targetId, useYn)
	 * @return
	 * @throws SQLException
	 */
	public int updateListAttachFile(AttachFileVO attachFile){
		int result = 0;
		try{
			result = update("AttachFile.updateListAttachFileUseYn", attachFile);
			logger.debug("AttachFile.updateListAttachFileUseYn fileName:{} result:{}", attachFile.getAttachFileNm(), result);
		}catch(Exception e){
			logger.error("cause:{}", e.toString());
		}
		return result;
	}
	
	/**
	 * deleteAttachFile
	 * AttachFile 삭제
	 * @param attachFile
	 * @return
	 * @throws SQLException
	 */
	public int deleteAttachFile(AttachFileVO attachFile){
		int result = 0;
		try{
			result = delete("AttachFile.deleteAttachFile", attachFile);
			if(result > 0){
				logger.debug("AttachFile.deleteAttachFile SUCCESS:{}", result);
			}else{
				logger.debug("AttachFile.deleteAttachFile FAIL:{}", result);
			}
		}catch(Exception e){
			logger.error("cause:{}", e.toString());
		}
		return result;
	}
	
	/**
	 * deleteAttachFile
	 * AttachFile 삭제
	 * @param attachFile
	 * @return
	 * @throws SQLException
	 */
	public int deleteContentsAttachFile(AttachFileVO attachFile){
		int result = 0;
		try{
			result = delete("AttachFile.deleteContentsAttachFile", attachFile);
			if(result > 0){
				logger.debug("AttachFile.deleteAttachFile SUCCESS:{}", result);
			}else{
				logger.debug("AttachFile.deleteAttachFile FAIL:{}", result);
			}
		}catch(Exception e){
			logger.error("cause:{}", e.toString());
		}
		return result;
	}
	
	/**
	 * deleteListAttachFile
	 * 대상테이블과 ID에 해당하는 AttachFile List 삭제
	 * @param SearchVO
	 * @return success delete row count 
	 * @throws SQLException
	 */
	public int deleteListAttachFile(String targetTable, String targetId){
		int result = 0;
		try{
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("where", String.format("target_table='%s' and target_id = %s", targetTable, targetId));
			result = delete("AttachFile.deleteListAttachFile", map);//삭제한 row수 반환
			if(result > 0){
				logger.debug("AttachFile.deleteListAttachFile SUCCESS:{}", result);
			}else{
				logger.debug("AttachFile.deleteListAttachFile FAIL:{}", result);
			}
		}catch(Exception e){
			logger.error("cause:{}", e.toString());
		}
		return result;
	}
	
	/**
	 * deleteListAttachFile
	 * AttachFile List 삭제
	 * @param SearchVO
	 * @return success delete row count 
	 * @throws SQLException
	 */
	public int deleteListAttachFile(AttachFileVO searchCondition){
		int result = 0;
		try{
			result = delete("AttachFile.deleteListAttachFile", searchCondition);//삭제한 row수 반환
			if(result > 0){
				logger.debug("AttachFile.deleteListAttachFile SUCCESS:{}", result);
			}else{
				logger.debug("AttachFile.deleteListAttachFile FAIL:{}", result);
			}
		}catch(Exception e){
			logger.error("cause:{}", e.toString());
		}
		return result;
	}
	
	/**
	 * retrieveListCountAttachFile
	 * AttachFile 검색결과 수
	 * @param searchCondition
	 * @return
	 * @throws SQLException
	 */
	public int retrieveListCountAttachFile(AttachFileVO searchCondition){
		int count = 0;
		try {
			
			count = ((Integer)getSqlMapClientTemplate().queryForObject("AttachFile.retrieveListCountAttachFile", searchCondition)).intValue();
			logger.debug("AttachFile.retrieveListCountAttachFile count:{}", count);
		} catch (Exception e) {
			logger.error("cause:{}", e.toString());   
		} 	
		return count;
	}
	
	/**
	 * retrieveListAttachFile
	 * AttachFile 검색
	 * @param attachFile
	 * @return
	 * @throws SQLException
	 */
	@SuppressWarnings("unchecked")
	public List<AttachFileVO> retrieveListAttachFile(AttachFileVO searchCondition){
		List<AttachFileVO> list = new ArrayList<AttachFileVO>();
		try{
			list = list("AttachFile.retrieveListAttachFile", searchCondition);
			logger.debug("AttachFile.retrieveListAttachFile count:{}", list.size());
		}catch(Exception e){
			logger.error("cause:{}", e.toString()); 
		}
		return list;
	}
}